/*
path: (7) [input, div.content, div#container, body, html, document, Window]

↓

html body div#container div.content input
*/

function getSelector(eventPath) {
  return eventPath
    .reverse()
    .filter(el => {
      return el !== document && el !== window;
    })
    .map(el => {
      let selector = '';
      if (el.id) {
        return `${el.nodeName.toLowerCase()}#${el.id}`;
      } else if (el.className && typeof el.className === 'string') {
        el.className.split(' ').filter(item => !!item/*过滤掉管空白符*/).join('.');
        return `${el.nodeName.toLowerCase()}.${el.className}`;
      } else {
        selector = el.nodeName.toLowerCase();
      }
      return selector;
    })
    .join(' ');
}

export default function (eventPathOrTarget) {
  if (Array.isArray(eventPathOrTarget)) {
    return getSelector(eventPathOrTarget)

  }else {
    const path = [];
    while (eventPathOrTarget){
      path.push(eventPathOrTarget);
      eventPathOrTarget = eventPathOrTarget.parentNode;
    }
    return getSelector(path)
  }
}
